
CC		= gcc.exe
ASM		= nasm.exe
PYTHON	= python.exe
LD  	= ld.exe
STRIP 	= strip.exe
OBJFORM	= win32
MAKE 	= mingw32-make.exe
CFLAGS	:= -c -m32 -nostdlib -fno-stack-protector -fno-builtin -W -Wall -Wstrict-prototypes -Wmissing-prototypes
LDFLAGS = -Ttext 0xc0400000 -e ___main -m i386pe -s
EXEFLAG = -m32 -nostdlib -fno-stack-protector -fno-builtin -W -Wall -Wstrict-prototypes -Wmissing-prototypes

BUILD_PATH 	= build
KERNEL_PATH = kernel
LIB_PATH	= lib
DEV_PATH 	= device
FS_PATH		= filesystem

DEVOBJS :=	$(BUILD_PATH)/8254.o $(BUILD_PATH)/8259A.o $(BUILD_PATH)/CMOS.o $(BUILD_PATH)/KBController.o $(BUILD_PATH)/PowerMgr.o $(BUILD_PATH)/HardDisk.o
LIBOBJS :=  $(BUILD_PATH)/rtl.o $(BUILD_PATH)/heap.o $(BUILD_PATH)/handle.o $(BUILD_PATH)/hal.o
KEROBJS := 	$(BUILD_PATH)/spinlock.o $(BUILD_PATH)/interrupt.o $(BUILD_PATH)/thread.o $(BUILD_PATH)/process.o $(BUILD_PATH)/memory.o $(BUILD_PATH)/syscall.o
FSOBJS  := $(BUILD_PATH)/file.o

OBJS 	:=  $(BUILD_PATH)/main.o $(BUILD_PATH)/kernel.o $(DEVOBJS) $(LIBOBJS) $(KEROBJS) $(FSOBJS)

EXE		= main.exe

ARCH  	= x86
IMG		= hd.img
VM		= qemu.exe

.PHONY:clean init build reexec rebuild exec

build:init $(EXE)
	$(PYTHON) MakeDisk.py $(IMG) 0 arch/$(ARCH)/mbr.bin 2 arch/$(ARCH)/loader.bin 16 $(EXE)

init:arch/$(ARCH)/mbr.bin arch/$(ARCH)/loader.bin

arch/$(ARCH)/mbr.bin:arch/$(ARCH)/mbr.S
	$(ASM) arch/$(ARCH)/mbr.S -f bin -o arch/$(ARCH)/mbr.bin

arch/$(ARCH)/loader.bin:arch/$(ARCH)/loader.S
	$(ASM) arch/$(ARCH)/loader.S -f bin -o arch/$(ARCH)/loader.bin

$(EXE):$(OBJS)
	$(LD) $(OBJS) $(LDFLAGS) -o $(EXE)

$(BUILD_PATH)/main.o:$(KERNEL_PATH)/main.c
	$(CC) $(CFLAGS) $< -o $@

$(DEVOBJS): $(BUILD_PATH)/%.o:$(DEV_PATH)/%.c $(DEV_PATH)/%.h
	$(CC) $(CFLAGS) $< -o $@

$(LIBOBJS): $(BUILD_PATH)/%.o:$(LIB_PATH)/%.c $(LIB_PATH)/%.h
	$(CC) $(CFLAGS) $< -o $@

$(KEROBJS): $(BUILD_PATH)/%.o:$(KERNEL_PATH)/%.c $(KERNEL_PATH)/%.h
	$(CC) $(CFLAGS) $< -o $@

$(FSOBJS): $(BUILD_PATH)/%.o:$(FS_PATH)/%.c $(FS_PATH)/%.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_PATH)/kernel.o:$(KERNEL_PATH)/kernel.S
	$(ASM) $< -f $(OBJFORM) -o $@

rebuild:clean build

reexec:rebuild
	$(VM) -hda $(IMG)

exec:build
	$(VM) -hda $(IMG) -hdb .\a.exe

clean:
	$(RM) $(OBJS) $(IMG) $(EXE) arch/$(ARCH)/mbr.bin arch/$(ARCH)/loader.bin
